//============================================================================
// Name        : player.cpp
// Author      : dfoelber
//============================================================================

#include "object/player.h"
#include "image_manager.h"
#include "draw/SDL_draw.h"

/**
* Constructor
* 
* @param image the image the player
* @param owner if the object is the owner of the surface (for cleanup)
**/
PlayerObject::PlayerObject( Surface* image, bool owner ) : Object( image, OBJECT_ID_PLAYER, owner )
{
	staticImage = image;
	animIndex = animCounter = 0;
	isCasting = false;
	colorKey = SDL_MapRGB(image->GetSurface()->format, 255, 255, 255);
	
	SetBackground( COLOR_BLACK, 0 );
	
	this->image = ImageManager::GetManager()->CreateImage(image->GetWidth(),image->GetHeight());
	SDL_SetColorKey(this->image->GetSurface(), SDL_SRCCOLORKEY | SDL_RLEACCEL, colorKey);
	Draw_FillRect( this->image->GetSurface(), 0, 0, image->GetWidth(), image->GetHeight(), COLOR_WHITE.ToUint32() );
	image->Apply(this->image, 0, 0);
	
	CreateAnimations();
}

/**
* Deconstructor
**/
PlayerObject::~PlayerObject()
{
	if (image)
	{
		delete image;
		image = NULL;
	}
}

/**
* Lets the object know that time has passed and to move accordingly.
* 
* @param ms The time passed since last called
**/
void PlayerObject::TimePassed( int ms )
{
	Object::TimePassed(ms);
	
	if( isCasting )
	{
		animCounter += ms;
		if( animCounter >= PLAYER_CASTING_ANIM_DELAY )
		{
			if( (int)anims.size() > 0 )
			{
				if( animIndex >= (int)anims.size() )
				{
					isCasting = false;
					Draw_FillRect( image->GetSurface(), 0, 0, image->GetWidth(), image->GetHeight(), COLOR_WHITE.ToUint32() );
					staticImage->Apply(image, 0, 0);
					return;
				}
				
				Draw_FillRect( image->GetSurface(), 0, 0, image->GetWidth(), image->GetHeight(), COLOR_WHITE.ToUint32() );
				anims[animIndex]->Apply(image, 0, 0);
				
				animIndex++;
				
			}
			
			animCounter = 0;
		}
	}
}

/**
* Starts a casting animation.
**/
void PlayerObject::StartCastAnimation()
{
	animCounter = animIndex = 0;
	isCasting = true;
}

/**
* Creates the animation vector
**/
void PlayerObject::CreateAnimations()
{
	anims.push_back( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_1) );
	anims.push_back( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_2) );
	anims.push_back( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_3) );
	anims.push_back( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_4) );
	anims.push_back( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_5) );
	anims.push_back( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_6) );
	anims.push_back( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_7) );
	anims.push_back( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_8) );
	anims.push_back( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_9) );
	anims.push_back( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_10) );
	anims.push_back( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_11) );
	anims.push_back( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_12) );
	anims.push_back( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_13) );
	anims.push_back( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_14) );
	anims.push_back( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_15) );
	anims.push_back( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_16) );
	anims.push_back( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_17) );
	anims.push_back( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_18) );
}
